const express = require("express");
const app = express();
const port = 3000;

const fs = require("fs");
// npm i md5
const md5 = require("md5");
const bodyParser = require("body-parser");
app.use(bodyParser.json());
const jwt = require("jsonwebtoken");

const user = require("./sql/user"); //员工  数据库集合
const user2 = require("./sql/user2"); //用户  数据库集合 手机 密码  age
const leave = require("./sql/leave");
const filmlist = require("./sql/filmlist");
const yonghu = require("./sql/yonghu");
var cors = require("cors");

app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));

//123   md5(123)  Jhi78   1 A向后推10位数 10J

const passwdCrypt = (req, res, next) => {
  console.log("passwdCrypt");
  console.log(req.body); //{ data: { password: 'abc123456', mobile: '18699991111' } }
  var passwd = req.body.password; //123456

  req.body.passwdCrypted = md5(passwd); //grgrgergergergerg
  next();
};

app.get("/prod-api/deleteUser", (req, res) => {
  // 数据库调用user.delteOne
  res.send("Hello World!");
});

app.get("/api/deleteUser", (req, res) => {
  // 数据库调用  userTest.deleteOne
  res.send("Hello World!");
});

app.post("/init", passwdCrypt, (req, res) => {
  res.send("init password is:" + req.body.passwdCrypted);
  // init password is:f5e441058f94f7318dc0【明文密码是123456】
});
const checkToken = function (req, res, next) {
  console.log("我进入校验token识别 这一步了");
  // 这一步相当于迪士尼买票的通过你来的
  // dfwefjlkgjiorjiowefkjfkjfsljkfsljefljjiowojwffejiojwfiewfjiovej
  //  dfsfweerkjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj 假token
  // {age:20,name:'zs',height:'190'，buy:true,time:'2021-6-4'}
  console.log("token");
  console.log(req.query.token);
  // 怎么样都可以传  这只是一种传递方式  能过来让服务器看见token就可以了
  var _token = req.query.token;

  //这里要做 更合理的  各种情况的处理
  try {
    // jwt引擎 反推这个秘文  是否是我给你的  是的话 把你秘文变成你的 用户信息 还原
    let verify = jwt.verify(_token, jwt_secret);
    console.log("verify");
    // 加密的时候
    // data:{
    //     // 转译用户信息的过程算法
    //     _token:jwt.sign({
    //         // userId:result.userId,
    //         password:passwdCrypted,
    //         mobile:result.mobile
    //     },
    //         jwt_secret,
    //         { expiresIn: 60*60*60}
    //     )
    // }
    // 解密的时候
    // verify {
    //     password: 'a1e99fa5594db03d4187704794a7acd4',
    //     mobile: '18612345678',
    //     iat: 1622789587,
    //     exp: 1623005587
    //   }
    console.log(verify);
    if (verify) {
      console.log("verify", verify);
      console.log("我进入verify里面了");
      req.body.user_info = verify;
      console.log("req.body.user._info", req.body.user_info);
      next();
    }
  } catch (error) {
    res.send({
      code: "888",
      info: "你是不是没有我们的权限token乱来啊请重新登陆 ",
    });
  }
};

app.get("/userinfo", checkToken, (req, res) => {
  console.log("进来了/api/v1/user_info");

  console.log(req.body.user_info);
  res.send({
    code: "1000",
    info: "success",
    info2: req.body.user_info,
  });
});
//  先命中注册 然后不往后执行 要执行 passwdCrypt函数
app.post("/zhuce", passwdCrypt, (req, res) => {
  console.log("zhuce我进来了");
  // 拿get请求的参数 req.query
  //post 请求 req.body  e10adc3949ba59abbe56e057f20f883e
  var obj = {
    mobile: req.body.mobile,
    // password:req.body.password,
    password: req.body.passwdCrypted,
  };
  user.findOne({ mobile: req.body.mobile }, (err, data) => {
    if (err) {
      console.log(err);
    }
    // 如果查到了 有data代表注册过  让她继续停留在注册页面 别管他‘

    if (data) {
      res.send({
        code: "409",
        message: "已经注册过了",
      });
    } else {
      // 如果没注册  开始注册 注册好了 进login页面 让他登录
      user.insertMany(obj, (err, data) => {
        if (err) {
          console.log("err", err);
        }
        console.log(data);
        //数据库有他就可以进入 /login
        res.send({
          code: "200",
          msg: data,
          message: "注册成功",
        });
      });
    }
  });
});

const jwt_secret = "aaa";
app.post("/api/login", passwdCrypt, async (req, res) => {
  console.log("/login");
  //这一步呼应 上面中间件处理的哪一步 req.body.passwdcrpyt = a
  let { mobile, passwdCrypted } = req.body;
  console.log("mobile", mobile);
  console.log("passwdcrpty", passwdCrypted);
  let result = await user.findOne({ mobile, password: passwdCrypted });
  if (result) {
    res.send({
      code: "1000",
      info: "success",
      mobile: mobile,
      // 445345435345345+一小时的时间戳
      //           {password:dfdfefef,mobile:1888888888 }
      //    {password:dfdfefef,mobile:1888888888, time:'1h'}
      // 下面的这个乱码和上面的对象意思一样 知识密码化了给前端
      // dfwefjlkgGGGiorjiowefkjfaaakjfslajkfslaaaaaajefljjiowojwffedjaaaiojwfi
      // dfwefjlkgGGGiorjiowefkjfaaakjfslajkfslaaaaaajefljjiowojwffedjaaaiojwfiewfjiovej
      data: {
        // 转译用户信息的过程算法
        _token: jwt.sign(
          {
            // userId:result.userId,
            password: passwdCrypted,
            mobile: result.mobile,
          },
          jwt_secret,
          { expiresIn: 1000 * 60 * 60 * 2 }
        ),
      },
    });
  } else {
    res.send({
      code: "9999",
      info: "走开 你不是我的用户 ",
    });
  }
});

// vue3 后台管理 API

// 用户列表获取API  pagenum 2   pagesize 5  第一页 一页三条
// { query: "", pagenum: 1, pagesize: 3, id: "" ,total:10}
// 员工
app.get("/api/sys/userlist", async (req, res) => {
  console.log("req.query", req.query);
  if (JSON.stringify(req.query) == "{}") {
    var userlist = await user.find({});
  } else {
    var userlist = await user
      .find({})
      .skip((req.query.pagenum - 1) * req.query.pagesize)
      .limit(req.query.pagesize * 1);
  }

  let userlist2 = await user.find({});
  console.log(userlist);
  res.send({
    info: "success",
    data: userlist,
    total: userlist2.length,
  });
});
// 用户
app.get("/api/sys/userlist2", async (req, res) => {
  console.log("req.query", req.query);
  if (JSON.stringify(req.query) == "{}") {
    var userlist = await yonghu.find({});
  } else {
    var userlist = await yonghu
      .find({})
      .skip((req.query.pagenum - 1) * req.query.pagesize)
      .limit(req.query.pagesize * 1);
  }

  let userlist2 = await yonghu.find({});
  console.log(userlist);
  res.send({
    info: "success",
    data: userlist,
    total: userlist2.length,
  });
});

// 请假数据分页

// app.get('/api/sys/userlistyonghu', async (req, res) => {
//   console.log('req.query', req.query)
//   if(JSON.stringify(req.query) == '{}') {
//     var userlist = await yonghu.find({})
//   }else {
//     var userlist = await user.find({}).skip((req.query.pagenum - 1) * req.query.pagesize).limit(req.query.pagesize * 1)
//   }

//   let userlist2 = await user.find({})
//   console.log(userlist);
//   res.send({
//     info: 'success',
//     data: userlist,
//     total: userlist2.length
//   })
// })

// 员工
app.get("/api/sys/getUsersearch", async (req, res) => {
  console.log("req.query.query", req.query);
  let newuser = await user.find({ mobile: req.query.mobile });

  res.send({
    info: "success",
    data: newuser,
  });
});
// 用户
app.get("/api/sys/getUsersearch2", async (req, res) => {
  console.log("req.query.query", req.query);
  let newuser = await yonghu.find({ mobile: req.query.mobile });

  res.send({
    info: "success",
    data: newuser,
  });
});

// 添加用户的方法
// export const addUser = (data) => {
//   return request({
//     url: '/sys/adduser',
//     method: 'post',
//     data:{
//       data:data
//     }
//   })
// }
// 用户
app.post("/api/sys/adduser", passwdCrypt, async (req, res) => {
  console.log("req.body /api/sys/adduser");
  console.log(req.body);
  var obj = {
    mobile: req.body.mobile,
    password: req.body.passwdCrypted,
    age: req.body.age,
    height: req.body.height,
    name: req.body.name,
    constellation: req.body.constellation,
  };
  console.log(obj);
  let newuser = await yonghu.find({ mobile: req.body.mobile });
  console.log("newuser", newuser.length);
  if (newuser.length != 0) {
    res.send({
      info: "fail",
      data: "已经注册过了",
    });
  } else {
    await yonghu.insertMany(obj);
    res.send({
      info: "success",
      data: "注册成功",
    });
  }
});

// 员工
app.post("/api/sys/addemployee", passwdCrypt, async (req, res) => {
  console.log("req.body /api/sys/addemployee");
  console.log(req.body);
  var obj = {
    mobile: req.body.mobile,
    password: req.body.passwdCrypted,
  };
  console.log(obj);
  let newuser = await user.find({ mobile: req.body.mobile });
  console.log("newuser", newuser.length);
  if (newuser.length != 0) {
    res.send({
      info: "fail",
      data: "已经注册过了",
    });
  } else {
    await user.insertMany(obj);
    res.send({
      info: "success",
      data: "注册成功",
    });
  }
});
// export const deluser = (data) => {
//   return request({
//     url: '/sys/deluser',
//     method: 'post',
//     data:{
//       data:data
//     }
//   })
// }
// 删除用户
app.post("/api/sys/deluser", async (req, res) => {
  console.log("req.body.data", req.body.data); //req.body deluser { data: '63573abf445ae3120c305245' }

  var result = await yonghu.findByIdAndDelete(req.body.data);
  console.log("result");
  console.log(result);
  res.send({
    info: "success",
    data: "删除成功",
  });
});
// 删除员工
app.post("/api/sys/delemp", async (req, res) => {
  console.log("req.body.data", req.body.data); //req.body deluser { data: '63573abf445ae3120c305245' }

  var result = await user.findByIdAndDelete(req.body.data);
  console.log("result");
  console.log(result);
  res.send({
    info: "success",
    data: "删除成功",
  });
});

// 更新用户api
app.post("/api/sys/updateuser", async (req, res) => {
  console.log("req.body updateuser", req.body);

  var result = await yonghu.findByIdAndUpdate(req.body.data._id, req.body.data);
  res.send({
    info: "success",
    data: "更新成功",
  });
});
// 更新员工api
app.post("/api/sys/update", async (req, res) => {
  console.log("req.body updateemp", req.body);

  var result = await user.findByIdAndUpdate(req.body.data._id, req.body.data);
  res.send({
    info: "success",
    data: "更新成功",
  });
});

// 局部中间件  拦路虎
// req.body.passwdCrypted = md5(passwd); //grgrgergergergerg
//   next();
app.post("/api/sys/login", passwdCrypt, async (req, res) => {
  console.log("/login");
  //这一步呼应 上面中间件处理的哪一步 req.body.passwdcrpyt = a
  // 14e1b600b1fd579f47433b88e8d85291
  let { mobile, passwdCrypted } = req.body;
  console.log("req.body", req.body);
  console.log("mobile", mobile);
  console.log("passwdcrpty", passwdCrypted);
  let result = await user.findOne({ mobile: mobile, password: passwdCrypted });
  if (result) {
    res.send({
      code: "1000",
      info: "success",
      mobile: mobile,

      data: {
        // 转译用户信息的过程算法
        _token: jwt.sign(
          {
            // userId:result.userId, fefefefefefefefefefefefefefeggg
            // jwt.verify (fefefefefefefefefefefefefefeggg)
            password: passwdCrypted,
            mobile: result.mobile,
          },
          jwt_secret,
          { expiresIn: 10 * 60 * 60 * 60 * 60 }
        ),
      },
    });
  } else {
    res.send({
      code: "9999",
      info: "走开 你不是我的用户 ",
    });
  }
});
// {
//   // userId:result.userId, fefefefefefefefefefefefefefeggg
//   // jwt.verify (fefefefefefefefefefefefefefeggg)
//   password: passwdCrypted,
//   mobile: result.mobile,
// }
const checkToken2 = function (req, res, next) {
  console.log("我进入校验token识别 这一步了");
  console.log("token");
  console.log("req.headers.Authorization", req.headers.authorization.substr(7));
  var _token = req.headers.authorization.substr(7);

  try {
    // jwt引擎 反推这个秘文  是否是我给你的  是的话 把你秘文变成你的 用户信息 还原
    let verify = jwt.verify(_token, jwt_secret);
    // fefwefwefwefweggwegegwgwgwgwegggggggg
    // verify = {
    //   password: fefefeffewfefweffef,
    //   mobile: 18612345679,
    // }
    console.log("verify");
    console.log(verify);
    if (verify) {
      console.log("verify", verify);
      console.log("我进入verify里面了");
      req.body.user_info = verify;
      console.log("req.body.user._info", req.body.user_info);
      next();
    }
  } catch (error) {
    res.send({
      code: "403",
      info: "你是不是没有我们的权限token乱来啊请重新登陆 ",
    });
  }
};

// checkToken2 拦路虎 中间件   checkToken2   局部中间件
app.get("/api/sys/profile", checkToken2, (req, res) => {
  console.log("进来了/api/v1/user_info");

  console.log(req.body.user_info.mobile);

  if (req.body.user_info.mobile == "18612345678") {
    var rights = [
      {
        id: 1,
        authName: "个人相关",
        children: [
          {
            id: 11,
            authName: "个人中心",
            path: "/menu/one",
          },
        ],
      },
      {
        id: 2,
        authName: "用户",
        children: [
          {
            id: 13,
            authName: "员工管理",
            path: "/menu/two",
          },
          {
            id: 14,
            authName: "用户管理",
            path: "/menu/three",
          },
          {
            id: 15,
            authName: "互联影库",
            path: "/menu/four",
          },
        ],
      },
      {
        id: 3,
        authName: "销售",
        children: [
          {
            id: 21,
            authName: "销售排名",
            path: "/menu/five",
          },
          {
            id: 22,
            authName: "销售信息",
            path: "/menu/six",
          },
        ],
      },
      {
        id: 4,
        authName: "工资管理",
        children: [
          {
            id: 23,
            authName: "员工工资",
            path: "/menu/seven",
          },
          {
            id: 24,
            authName: "高管工资",
            path: "/menu/eight",
          },
        ],
      },
      {
        id: 5,
        authName: "请假审批",
        children: [
          // {
          //   id: 25,
          //   authName: "审批信息",
          //   path: "/menu/nine",
          // },
        ],
      },
      {
        id: 6,
        authName: "人事管理",
        children: [
          {
            id: 27,
            authName: "员工管理",
            path: "/menu/eleven",
          },
          {
            id: 28,
            authName: "请假管理",
            path: "/menu/twelve",
          },
        ],
      },
    ];

    res.send({
      code: 200,
      info: "success",
      data: {
        avatar:
          "https://th.bing.com/th/id/OIP.EaGvrG_JXcnpTnCpFBYQbwAAAA?w=211&h=211&c=7&r=0&o=5&pid=1.7",
        id: "0",
        mobile: "18612345678",
        name: "董事长1",
      },
      rights: rights,
      button1: true,
      button2: true,
      button3: true,
      button4: true,
      permission: {
        menus: [
          "userManage",
          "roleList",
          "permissionList",
          "articleRanking",
          "articleCreate",
        ],
        points: [
          "distributeRole",
          "importUser",
          "removeUser",
          "distributePermission",
        ],
      },
      role: [{ id: "0", title: "Super administrator" }],
      title: "超级管理员",
      _id: "wfefwe4644efwef",
    });
  } else if (req.body.user_info.mobile == "18612345679") {
    var rights = [
      {
        id: 1,
        authName: "个人相关",
        children: [
          {
            id: 11,
            authName: "个人中心",
            path: "/menu/one",
          },
        ],
      },
      {
        id: 2,
        authName: "用户",
        children: [
          {
            id: 13,
            authName: "员工管理",
            path: "/menu/two",
          },
          {
            id: 14,
            authName: "用户管理",
            path: "/menu/three",
          },
          {
            id: 15,
            authName: "互联影库",
            path: "/menu/four",
          },
        ],
      },
      {
        id: 3,
        authName: "文章",
        children: [
          {
            id: 21,
            authName: "文章排名",
            path: "/menu/five",
          },
          {
            id: 22,
            authName: "创建文章",
            path: "/menu/six",
          },
        ],
      },

      {
        id: 5,
        authName: "请假申请",
        children: [
          {
            id: 25,
            authName: "审批信息",
            path: "/menu/nine",
          },
        ],
      },
      {
        id: 6,
        authName: "电影管理",
        children: [
          {
            id: 26,
            authName: "互联影库",
            path: "/menu/ten",
          },
        ],
      },
    ];

    res.send({
      code: 200,
      info: "success",
      data: {
        avatar:
          "https://th.bing.com/th/id/OIP.f_kkQnSONML_FOAtUzFxXQHaHa?w=190&h=190&c=7&r=0&o=5&pid=1.7",
        id: "0",
        mobile: "18612345679",
        name: "王波",
      },
      rights: rights,
      button1: true,
      button2: true,
      button3: false,
      button4: false,
      permission: {
        menus: [
          "userManage",
          "roleList",
          "permissionList",
          "articleRanking",
          "articleCreate",
        ],
        points: [
          "distributeRole",
          "importUser",
          "removeUser",
          "distributePermission",
        ],
      },
      role: [{ id: "0", title: "Super administrator" }],
      title: "中层",
      _id: "wfefwe4644efwef",
    });
  } else if (req.body.user_info.mobile == "13807347079") {
    var rights = [
      {
        id: 1,
        authName: "个人相关",
        children: [
          {
            id: 11,
            authName: "个人中心",
            path: "/menu/one",
          },
        ],
      },
      {
        id: 3,
        authName: "文章",
        children: [
          {
            id: 21,
            authName: "文章排名",
            path: "/menu/five",
          },
          {
            id: 22,
            authName: "创建文章",
            path: "/menu/six",
          },
        ],
      },
    ];

    res.send({
      code: 200,
      info: "success",
      data: {
        avatar:
          "https://vkceyugu.cdn.bspapp.com/VKCEYUGU-0113d566-d450-4ea0-839d-c4972a6140fd/2fecb828-7a6e-4f53-95f9-0e9378a5dbe9.png",
        id: "0",
      },
      rights: rights,
      permission: {
        menus: [
          "userManage",
          "roleList",
          "permissionList",
          "articleRanking",
          "articleCreate",
        ],
        points: [
          "distributeRole",
          "importUser",
          "removeUser",
          "distributePermission",
        ],
      },
      role: [{ id: "0", title: "Super administrator" }],
      title: "基层",
      _id: "wfefwe4644efwef",
    });
  }
});
// 休假申请相关API
app.post("/api/sys/addleaveinfo", async (req, res) => {
  console.log(req.body);
  await leave.insertMany(req.body.data);
  res.send({
    info: "success",
    data: "信息创建成功",
  });
});

// app.get("/api/sys/userlist2", async (req, res) => {
//   console.log("req.query", req.query);
//   if (JSON.stringify(req.query) == "{}") {
//     var userlist = await yonghu.find({});
//   } else {
//     var userlist = await yonghu
//       .find({})
//       .skip((req.query.pagenum - 1) * req.query.pagesize)
//       .limit(req.query.pagesize * 1);
//   }

//   let userlist2 = await yonghu.find({});
//   console.log(userlist);
//   res.send({
//     info: "success",
//     data: userlist,
//     total: userlist2.length,
//   });
// });

app.get("/api/sys/showleaveinfo", async (req, res) => {
  console.log("/api/sys/showleaveinfo");
  let list = await leave.find({});
  res.send({
    info: "success",
    data: list,
  });
});

app.post("/api/sys/delleaveinfo", async (req, res) => {
  console.log("/api/sys/sdelleaveinfo");
  console.log(req.body);
  await leave.findByIdAndDelete({ _id: req.body._id });
  res.send({
    info: "success",
  });
});

app.post("/api/sys/updateleaveinfo", async (req, res) => {
  console.log("uuuuu/api/sys/updateleaveinfo");
  console.log(req.body);
  var newobj = req.body.data;
  newobj.leavestatus = false;
  console.log("newobj");
  console.log(newobj);
  await leave.findByIdAndUpdate({ _id: req.body.data._id }, newobj);
  res.send({
    info: "success",
  });
});

app.get("/api/sys/findleaveinfo", async (req, res) => {
  console.log("/api/sys/findleaveinfo");
  console.log(req.query);
  let list = await leave
    .find({})
    .skip((req.query.pagenum - 1) * req.query.pagesize)
    .limit(req.query.pagesize * 1);
  let list1 = await leave.find({});
  console.log(list);
  res.send({
    info: "success",
    list,
    total: list1.length,
  });
});

// 创建电影 相关  const filmlist = require('./sql/filmlist')

app.post("/api/sys/addfilminfo", async (req, res) => {
  console.log(req.body);
  await filmlist.insertMany(req.body.data);
  res.send({
    info: "success",
    data: "信息创建成功",
  });
});

//获取所有电影

app.get("/api/sys/searchfilm", async (req, res) => {
  console.log("searchfilm");
  var result = await filmlist.find();
  res.send({
    info: "success",
    data: result,
  });
});

//根据type返回电影
app.post("/api/sys/searchfilmtype", async (req, res) => {
  console.log("searchfilmtype");
  console.log(req.body.type);
  const type = req.body.type;
  if (type === "全部") {
    var result = await filmlist.find();
  } else {
    var result = await filmlist.find({ type });
  }
  res.send({
    info: "success",
    data: result,
    total: result.length,
  });
});

//根据_id删除电影
app.post("/api/sys/delfilm", async (req, res) => {
  console.log("/api/sys/delfilm");
  console.log(req.body);
  await filmlist.findByIdAndDelete({ _id: req.body._id });
  res.send({
    info: "success",
  });
});
//分页接口
app.get("/api/sys/findfilminfo", async (req, res) => {
  console.log("/api/sys/findfilminfo");
  console.log(req.query);
  const type = req.query.type;
  console.log(type.value);
  if (type === "全部" || type === "") {
    var list = await filmlist
      .find({})
      .skip((req.query.pagenum - 1) * req.query.pagesize)
      .limit(req.query.pagesize * 1);
    var list1 = await filmlist.find({});
  } else {
    var list = await filmlist
      .find({ type })
      .skip((req.query.pagenum - 1) * req.query.pagesize)
      .limit(req.query.pagesize * 1);
    var list1 = await filmlist.find({ type });
  }
  console.log(list);
  res.send({
    info: "success",
    list,
    total: list1.length,
  });
});

//修改接口
app.post("/api/sys/updatefilminfo", async (req, res) => {
  console.log("uuuuu/api/sys/updatefilminfo");
  console.log(req.body);
  var newobj = req.body;
  console.log("newobj");
  console.log(newobj);
  await filmlist.findByIdAndUpdate({ _id: req.body._id }, newobj);
  res.send({
    info: "success",
  });
});
//搜索电影名字
app.get("/api/sys/searchfilmname", async (req, res) => {
  console.log("searchfilmname");
  console.log(req.query);
  const result = await filmlist.find({ name: req.query.name });
  if (result) {
    res.send({
      info: "success",
      data: result,
      total: result.length,
    });
  } else {
    res.send({
      info: "failed",
    });
  }
});

//前台用户注册逻辑

app.post("/customer/zhuce", passwdCrypt, (req, res) => {
  console.log("zhuce我进来了");
  // 拿get请求的参数 req.query
  //post 请求 req.body
  var obj = {
    mobile: req.body.mobile,
    // password:req.body.password,
    password: req.body.passwdCrypted,
  };
  yonghu.findOne({ mobile: req.body.mobile }, (err, data) => {
    if (err) {
      console.log(err);
    }
    // 如果查到了 有data代表注册过  让她继续停留在注册页面 别管他‘

    if (data) {
      res.send({
        info: "success",
        code: "409",
        message: "已经注册过了",
      });
    } else {
      // 如果没注册  开始注册 注册好了 进login页面 让他登录
      yonghu.insertMany(obj, (err, data) => {
        if (err) {
          console.log("err", err);
        }
        console.log(data);
        //数据库有他就可以进入 /login
        res.send({
          info: "success",
          code: "200",
          msg: data,
          message: "注册成功",
        });
      });
    }
  });
});

//前台用户登录逻辑

app.post("/customer/login", passwdCrypt, async (req, res) => {
  console.log("/login");
  //这一步呼应 上面中间件处理的哪一步 req.body.passwdcrpyt = a
  let { mobile, passwdCrypted } = req.body;
  console.log("mobile", mobile);
  console.log("passwdcrpty", passwdCrypted);
  let result = await yonghu.findOne({ mobile, password: passwdCrypted });
  if (result) {
    res.send({
      code: "1000",
      info: "success",
      mobile: mobile,
      // 445345435345345+一小时的时间戳
      //           {password:dfdfefef,mobile:1888888888 }
      //    {password:dfdfefef,mobile:1888888888, time:'1h'}
      // 下面的这个乱码和上面的对象意思一样 知识密码化了给前端
      // dfwefjlkgGGGiorjiowefkjfaaakjfslajkfslaaaaaajefljjiowojwffedjaaaiojwfi
      // dfwefjlkgGGGiorjiowefkjfaaakjfslajkfslaaaaaajefljjiowojwffedjaaaiojwfiewfjiovej
      data: {
        // 转译用户信息的过程算法
        _token: jwt.sign(
          {
            // userId:result.userId,
            password: passwdCrypted,
            mobile: result.mobile,
          },
          jwt_secret,
          { expiresIn: 60 * 60 * 60 * 60 }
        ),
      },
    });
  } else {
    res.send({
      info: "success",
      code: "9999",
      message: "走开 你不是我的用户 ",
    });
  }
});

//获取前台用户列表

app.get("/api/sys/getcustomerlist", async (req, res) => {
  console.log("getcustomerlist");
  console.log("req.query", req.query);
  const mobile = req.query.mobile;
  if (req.query.mobile) {
    var userlist = await yonghu
      .find({ mobile })
      .skip((req.query.pagenum - 1) * req.query.pagesize)
      .limit(req.query.pagesize * 1);
    var userlist2 = await yonghu.find({ mobile });
  } else {
    var userlist = await yonghu
      .find({})
      .skip((req.query.pagenum - 1) * req.query.pagesize)
      .limit(req.query.pagesize * 1);
    var userlist2 = await yonghu.find({});
  }
  console.log(userlist);
  res.send({
    info: "success",
    data: userlist,
    total: userlist2.length,
  });
});

//修改用户信息
app.post("/api/sys/updatecustomer", async (req, res) => {
  console.log("req.body updateuser", req.body);

  var result = await yonghu.findByIdAndUpdate(req.body._id, req.body);
  res.send({
    info: "success",
    data: "更新成功",
  });
});

//删除用户信息
app.post("/api/sys/delcustomer", async (req, res) => {
  console.log("req.body deluser", req.body);

  var result = await yonghu.findByIdAndDelete(req.body._id);
  console.log(result);
  res.send({
    info: "success",
    data: "删除成功",
  });
});

//卖座网前端数据渲染
app.get("/sys/filmlistshow", async (req, res) => {
  console.log("api/sys/filmlistshow");
  console.log(req.query);
  const length = (await filmlist.find({ type: req.query.type })).length;
  console.log(length);
  var result = await filmlist
    .find({ type: req.query.type })
    .limit(5)
    .skip((req.query.pagenum - 1) * 5);
  console.log(result.length);
  res.send({
    info: "success",
    data: result,
  });
});

//详情页数据返回
app.get("/sys/filmdetail", async (req, res) => {
  console.log("/sys/filmdetail");
  console.log(req.query);
  var result = await filmlist.find({ _id: req.query.id });
  res.send({
    info: "success",
    data: result,
  });
});

app.listen(port, () =>
  console.log(`Server is running at http://127.0.0.1:${port}!`)
);

// 你去迪士尼乐园玩
// {name:'zs',age:18,time:'12/28',mobile:188888}     aaa
// 给了你一张纸条
// fdsfdsaaaafsfwerrwe34aaa53543534aaa53453453aaa453453

// 中午出去吃饭
// 吃完饭进园区  fdsfdsfsfwerrwe345354353453453453453453  aaa 放在电脑里面进行反解
